// opencvTests.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <opencv2/core/core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include <algorithm>

const std::string filepath = "C:\\Users\\William\\Documents\\Faculdade\\ex_squares.jpg";

bool automatic_threshold(const cv::Mat& img, cv::Mat& imgOut,const double variation)
{
	//const int initial_threshold = 127;
	cv::Mat &mask = imgOut;

 
	
	double current_threshold = 127;//*cv::mean(img).val;
	double last_threshold = -1;
	for(int n = 0 ;std::abs(last_threshold - current_threshold) > variation || n < 1 ;n++)
	{
		mask = cv::Mat::zeros(img.cols,img.rows,0);
		cv::threshold(img,mask,current_threshold,255,cv::THRESH_BINARY);

		cv::Scalar mean0 = cv::mean(img,mask);
		cv::Scalar mean1 = cv::mean(img,255-mask);		
		
		last_threshold = current_threshold;
		current_threshold = *cv::Scalar(cv::Scalar(0.5) * (mean0 + mean1)).val;
	}
	

	return true;
}



const std::string images[] = 
{
	"D:\\backup\\Pictures\\girls\\MarisolRibeiro\\826725359_22002_122_505lo.JPG",
	"C:\\Users\\William\\Documents\\Faculdade\\peppers.bmp",
	"D:\\backup\\Pictures\\girls\\MarianaRios\\91599_mrios_prn_09_09_10_01_122_536lo_gray.jpg",
	"C:\\Users\\William\\Documents\\Faculdade\\ex_squares.jpg",
	"C:\\Users\\William\\Pictures\\Ponfac\\META\\GO\\Imagens_GO\\0NH7.jpg",
	"C:\\Users\\William\\Documents\\Faculdade\\digital.bmp"
};

void create_histogram_image(const cv::Mat &hist, cv::Mat &imgHist)
{
	imgHist = cv::Mat::zeros(512,512,0);

	const int bins = 256;

	float* max_bin_value = std::max_element((float*)hist.data,(float*)hist.data + 256);
	
	for(int x = 0 ; x < imgHist.cols;x++)
	{
		if(x %2 == 0)
		{
			float binValue = hist.at<float>(x/2);

			if(binValue > 0)
			{
				int plotHeight = binValue * imgHist.rows / *max_bin_value ;
				//if(plotHeight < 1)plotHeight = 1;
				//if(plotHeight > 512)plotHeight = 512;
				for(int y = 512 -1 ; y > 512 - plotHeight ;y--)
				{
					imgHist.at<byte>(cv::Point(x,y)) = 255;
				}
			}
		}
	}


}

/*
int _tmain(int argc, _TCHAR* argv[])
{
	cv::Mat img = cv::imread(images[5],0);
	cv::Mat imgOut;

	automatic_threshold(img,imgOut,2);

	//cv::threshold(img,imgOut,12,255,cv::THRESH_BINARY|cv::THRESH_OTSU);


	cv::imshow("imgNormal",img);
	cv::imshow("imgThr",imgOut);
	cv::waitKey();
	//func();

	return 1;
	try
	{
		const int threshold_inicial = 127;
		cv::Mat img = cv::imread(images[3],0);
		cv::Mat maskImg(img.rows,img.cols,0);
		cv::Mat hist;

	
		float range[] = { 0, 256 } ;
		const float* histRange = { range };

		const int histSize = 256;

		cv::calcHist(&img,1,0,cv::Mat(),hist,1,&histSize,&histRange);
		//calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
		
		cv::Mat histImg;
		create_histogram_image(hist,histImg);

		cv::imshow("Berna",histImg);

		cv::waitKey(0);
	}catch(std::exception &ex)
	{
		printf(ex.what());
	}
	catch(...)
	{
		printf("WTF");
	}
	
	

	return 0;
}


*/